package com.devg.file.client.core;

import com.devg.file.client.core.entity.FileClientConfig;
import com.devg.file.client.core.enums.RequestType;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;

public class WebHttpPost {

  private static final Integer timeOut = 30000;

  public HttpPost getHttpPost(FileClientConfig fileClientConfig, String requestType) {
    Integer socketTimeout = fileClientConfig.getSocketTimeout();
    Integer connectTimeout = fileClientConfig.getConnectTimeout();
    Integer connectionRequestTimeout = fileClientConfig.getConnectionRequestTimeout();
    RequestConfig requestConfig = RequestConfig.custom()
        .setSocketTimeout(socketTimeout == null ? timeOut : socketTimeout)
        .setConnectTimeout(connectTimeout == null ? timeOut : connectTimeout)
        .setConnectionRequestTimeout(connectionRequestTimeout == null ? timeOut : connectionRequestTimeout)
        .build();
    String uploadUrl = fileClientConfig.getUploadUrl();
    if (null != requestType && requestType.equals(RequestType.UPLOAD_FILE.getRequestType())) {
      uploadUrl += RequestType.UPLOAD_FILE.getRequestMethodName();
    } else if (null != requestType && requestType.equals(RequestType.UPLOAD_FILE_RANDOM.getRequestType())) {
      uploadUrl += RequestType.UPLOAD_FILE_RANDOM.getRequestMethodName();
    } else if (null != requestType && requestType.equals(RequestType.DELETE_FILE.getRequestType())) {
      uploadUrl += RequestType.DELETE_FILE.getRequestMethodName();
    }
    HttpPost httpPost = new HttpPost(uploadUrl);
    httpPost.setConfig(requestConfig);
    return httpPost;
  }

  public static WebHttpPost getInstance() {
    return new WebHttpPost();
  }
}
